//
//  MyTableViewController.m
//  13-HomeWork
//
//  Created by dllo on 16/8/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MyTableViewController.h"
#import "UIView+Frame.h"
#import "MyTableViewCell.h"
#define kTableViewCellReuse @"reuse"
@interface MyTableViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)NSArray *imageArray;
@property (nonatomic,strong)UIPageControl *pageControll;
@end

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self createScrollView];
    [self createPageControll];

}
- (void)reloadData {
    _peopleImageName = [NSMutableArray arrayWithObjects:@"user", nil];
    _authorNameArray = [NSMutableArray arrayWithObjects:@"终身美丽的小猪", @"宏丹",@"笑话",@"呵 呵" ,nil];          
}
- (void)createScrollView {
    _imageArray = @[@"05",@"01",@"02",@"03",@"04",@"05",@"01"];
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.pf_width, 300)];
    _headViewForScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.pf_width, _headView.pf_height * 3 / 5)];
    [_headView addSubview:_headViewForScrollView];
    for (int i = 0; i < _imageArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.pf_width, 0, self.view.pf_width, _headViewForScrollView.pf_height)];
        imageView.image = [UIImage imageNamed:_imageArray[i]];
        [_headViewForScrollView addSubview:imageView];
    }
    _headViewForScrollView.delegate = self;
    _headViewForScrollView.contentSize = CGSizeMake(self.view.pf_width * _imageArray.count, _headViewForScrollView.pf_height);
    _headViewForScrollView.contentOffset = CGPointMake(self.view.pf_width, 0);
    _headViewForScrollView.pagingEnabled = YES;
    [self.view addSubview:_headViewForScrollView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == _headViewForScrollView) {
        NSInteger i = scrollView.contentOffset.x / self.view.pf_width;
    if (i == 0) {
        scrollView.contentOffset = CGPointMake((_imageArray.count - 1 - 1) * self.view.pf_width, 0);
        _pageControll.currentPage = _imageArray.count - 3;
        
    } else if (i == _imageArray.count - 1) {
        scrollView.contentOffset = CGPointMake(self.view.pf_width, 0);
        _pageControll.currentPage = 0;
    } else {
        scrollView.contentOffset = CGPointMake(i * self.view.pf_width, 0);
        _pageControll.currentPage = i - 1;
    }
        
    }
    
}
- (void)createPageControll {
    _pageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, _headViewForScrollView.pf_height - 60, self.view.pf_width, 60)];
    _pageControll.numberOfPages = _imageArray.count - 2;
    _pageControll.currentPage = 0;
    [self.view addSubview:_pageControll];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse forIndexPath:indexPath];
    
        
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
